package xyz.yolin.test;

import java.util.Stack;

public class fanzhaun {
    public static void main(String[] args) {
        System.out.println(reverse_("fsdssd"));
        System.out.println(reverse2("hello"));
    }

    public static String reverse_(String str){
        if (str==null||str.length()<=1){
            return str;
        }
        StringBuilder sb = new StringBuilder(str);
        StringBuilder reverse = sb.reverse();
        return reverse.toString();
    }

    public static String reverse2(String str){
        if (str==null||str.length()<=1){
            return  str;
        }
        char[] chars = str.toCharArray();
        Stack<Character> stack = new Stack<>();
        for (char c:chars){
            stack.push(c);
        }
        ////使用length暂存堆的大小，因为遍历的过程中，会改变堆的大小
        int length=stack.size();
        StringBuffer sb = new StringBuffer();
        for (int i=0;i<length;i++){
            sb.append(stack.pop());
        }
        return sb.toString();
    }
}
